package info.hexin.druid;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by infi.he on 2015/6/10.
 */
public class Test1 {
    public static void main(String[] args) {
//        testSimple();
        testAmoeba();
    }

    private static void testAmoeba() {

        final AtomicLong atomicLong = new AtomicLong(0);

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("db-properties.xml");
        final JdbcTemplate amoebaJdbcTemplate = (JdbcTemplate) applicationContext.getBean("amoebaJdbcTemplate");
        final String insertSql = "INSERT INTO test_1 (`longid`, `datetime`) VALUES (?, ?)";
        final String selectSql = "SELECT * FROM test_1 where longid = ?";
        System.out.println("11111111111111111");

        //select
//        for (int i = 0; i < 100; i++) {
//            Map<String, Object> stringObjectMap = amoebaJdbcTemplate.queryForMap(selectSql, new Object[]{0});
//            System.out.println(stringObjectMap);
//        }


        for (int i = 0; i < 10; i++) {
            new Thread() {
                @Override
                public void run() {
                    for (int j = 0; j < 100; j++) {
                        long andIncrement = atomicLong.getAndIncrement();
                        long dateTime = System.currentTimeMillis();
//                        System.out.println("id >>>> " + andIncrement + " long >>>> " + dateTime);
                        amoebaJdbcTemplate.update(insertSql, new Object[]{andIncrement, dateTime});
                    }
                }
            }.start();
        }
//
        for (int i = 0; i < 10; i++) {
            new Thread() {
                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        try {
                            Map<String, Object> stringObjectMap = amoebaJdbcTemplate.queryForMap(selectSql, new Object[]{atomicLong.get()});
                            stringObjectMap.put("now",System.currentTimeMillis());
                            System.out.println(stringObjectMap);
                        } catch (Exception e) {
//                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }

    }

    private static void testSimple() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("db-properties.xml");
        JdbcTemplate simpleJdbcTemplate = (JdbcTemplate) applicationContext.getBean("simpleJdbcTemplate");
        Map map = simpleJdbcTemplate.queryForMap("select 1");
        System.out.println(map);
    }
}
